﻿using System;
using System.IO;
using System.Text;
using System.Threading;

namespace tjb.Utility
{
    #region Log
    /// <summary>
    /// 用于输出调试期间的应用程序错误 方便调试
    /// 创建:唐敬波
    /// 2008-7-30
    /// </summary>
    public static class Log
    {
        /// <summary>
        /// 日志文件名称
        /// </summary>
        public static string CurFileName = "log.txt";
        /// <summary>
        /// 当前日志文件大小
        /// </summary>
        public static long CurFileSize = 512000L;
        /// <summary>
        /// 日志历史文件后缀名
        /// </summary>
        public static string HistoryFileExtension = ".log";
        /// <summary>
        /// 日志文件基目录
        /// </summary>
        public static string ApplicationBasePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        /// <summary>
        /// 获取目录的下文件总大小
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static long GetDirectoryLength(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                return 0L;
            }
            long num = 0L;
            DirectoryInfo info = new DirectoryInfo(dirPath);
            foreach (FileInfo info2 in info.GetFiles())
            {
                num += info2.Length;
            }
            DirectoryInfo[] directories = info.GetDirectories();
            if (directories.Length > 0)
            {
                for (int i = 0; i < directories.Length; i++)
                {
                    num += GetDirectoryLength(directories[i].FullName);
                }
            }
            return num;
        }


        /// <summary>
        /// 将日志文件名称，历史日志文件，文件大小设置为默认值
        /// </summary>
        public static void ResetParameter()
        {

            CurFileName = "log.txt";

            CurFileSize = 512000L;

            HistoryFileExtension = ".log";

            ApplicationBasePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        }

        private static void writeLog(string str)
        {

            string BasePath = ApplicationBasePath + "/Log/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            string fileName = BasePath + CurFileName;
            lock (typeof(Log))
            {
                FileInfo finfo = new FileInfo(fileName);
                StreamWriter writer = null;
                try
                {
                    if (!finfo.Directory.Exists)
                    {
                        finfo.Directory.Create();
                    }
                    if (((finfo != null) && finfo.Exists) && (finfo.Length > CurFileSize))
                    {
                        string path = BasePath + DateTime.Now.ToString("HH_mm_ss") + HistoryFileExtension;
                        while (File.Exists(path))
                        {
                            path = BasePath + DateTime.Now.ToString("HH_mm_ss") + "_Continuous" + HistoryFileExtension;
                            if (File.Exists(path))
                            {
                                Thread.Sleep(1000);
                            }
                        }
                        finfo.MoveTo(path);
                    }
                    writer = new StreamWriter(fileName, true, Encoding.UTF8, 1024);
                    writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff：" + System.Environment.NewLine) + str);

                }
                catch (IOException)
                {

                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Flush();
                        writer.Close();
                        writer.Dispose();
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sdouble"></param>
        public static void WriteLog(double sdouble)
        {
            writeLog(sdouble.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sInt"></param>
        public static void WriteLog(int sInt)
        {
            writeLog(sInt.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        public static void WriteLog(string str)
        {
            writeLog(str);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteLog(Exception ex)
        {
            if (null != ex)
                if (ex.InnerException != null)
                    writeLog(ex.ToString() + "\n" + ex.InnerException.ToString());
                else
                    writeLog(ex.ToString());
            else
                writeLog("Exception is null");
        }
    }
    #endregion
}
